package main

import (
	"fmt"
)

func main() {
	strs := []string{"abcabcbb", "bbbbb", "pwwkew", " "}

	for i := range strs {
		fmt.Println("========================================")
		fmt.Println(strs[i])
		fmt.Println(lengthOfLongestSubstring(strs[i]))
		fmt.Println("========================================\n\n")
	}
}

func lengthOfLongestSubstring(s string) int {
	sl := len(s)              //string length
	ss := map[string]string{} // string substring
	ml := 0                   //max length
	mss := ss
	pos := 0

	for i := pos; i < sl; i++ {
		is := string(s[i])
		_, ok := ss[is]
		if ok {
			ssl := len(ss)
			if ml < ssl {
				ml = ssl
				mss = ss
			}
			//清空继续
			ss = map[string]string{}
			//	is: is,
			//}
			i = pos
			pos++
		} else {
			ss[is] = is
		}
	}

	if len(ss) > ml {
		ssl := len(ss)
		if ml < ssl {
			ml = ssl
			mss = ss
		}
	}

	for i := range mss {
		fmt.Print(mss[i])
	}
	fmt.Println("")
	return ml
}
